package com.cy.member;

import java.awt.Point;
import java.util.ArrayList;

import com.cy.controller.MasterController;
import com.cy.member.parent.ChineseChessman;
/**
 * 象
 * @author CY
 */
public class Xiang extends ChineseChessman {
	
	/**
	 * @param who true红色，false黑色
	 */
	public Xiang(boolean who) {
		if(who) {//红车
			this.setBackground("image/红象.GIF");
		} else {//黑车
			this.setBackground("image/黑象.gif");
		}
		//生成棋子
		this.generateChessman(who);
	}

	/**
	 * 象的移动策略
	 * 先判断它是否走田字，判断方法(Math.abs(origin.x-site.x) == 2 && Math.abs(origin.y-site.y) == 2)
	 * 在限制它过河((origin.y >= 5 && site.y >= 5)||(origin.y <= 4 && site.y <= 4));
	 * 判断象眼是否被逼住
	 */
	@Override
	public boolean move(Point site, ArrayList<Point> points) {
		if(MasterController.gameType) {
			return jingdian(site, points);
		} else {
			return liuxing(site, points);
		}
	}

	/**
	 * 经典模式
	 * @param site
	 * @param points
	 * @return
	 */
	private boolean jingdian(Point site, ArrayList<Point> points) {
		//起点坐标
		Point origin = getChessman().getSite();
		//要求对角线运动，而且步长为2,不可过河
		if(Math.abs(origin.x-site.x) == 2 && Math.abs(origin.y-site.y) == 2 && ((origin.y >= 5 && site.y >= 5)||(origin.y <= 4 && site.y <= 4))){
			//象眼是否被逼住
			Point xiangyan = new Point((origin.x+site.x)/2,(origin.y+site.y)/2);
			if(points.contains(xiangyan)) {
				return false;
			}
			getChessman().setSite(site);
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 流行模式
	 * @param site
	 * @return
	 */
	private boolean liuxing(Point site, ArrayList<Point> points) {
		//起点坐标
		Point origin = getChessman().getSite();
		//要求对角线线运动，而且步长为2
		if(Math.abs(origin.x - site.x) == 2 && Math.abs(origin.y - site.y) == 2) {
			//象眼是否被逼住
			Point xiangyan = new Point((origin.x+site.x)/2,(origin.y+site.y)/2);
			if(points.contains(xiangyan)) {
				return false;
			}
			getChessman().setSite(site);
			return true;
		}
		return false;
	}
}
